home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_fclose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.1 KB  |  53 lines

  1. /*                f c l o s e
  2.  *
  3.  * This function will flush any buffered data for the specified
  4.  * stream, then close it. Buffers that have been allocated
  5.  * to the stream by the stdio library will be freed. Buffers
  6.  * that have been allocated explicitly by the user will not
  7.  * be freed. It is the user's responsibility to free these
  8.  * if so required. This function is called automagically from
  9.  * exit().
  10.  *
  11.  * The function returns 0 on success and EOF if an error was
  12.  * detected.
  13.  *
  14.  * Patchlevel 1.0
  15.  *
  16.  * Edit History:
  17.  */
  18.  
  19. #include "stdiolib.h"
  20.  
  21. /*LINTLIBRARY*/
  22.  
  23. int fclose(fp)
  24.  
  25. FILE *fp;                /* stream */
  26.  
  27. {
  28.   FILE **sp;                /* slot */
  29.   int close();                /* close a channel */
  30.   void free();                /* free allocated memory */
  31.  
  32. /* Free slot occupied by this stream */
  33.   if ((sp = _slot(fp)) == NULL)
  34.     return EOF;
  35.   if (sp - _iop > 2)
  36.     *sp = NULL;
  37.  
  38. /* Flush any pending buffers */
  39.   (void) fflush(fp);
  40.  
  41. /* Close this channel */
  42.   if (close(fp->_file))
  43.     return EOF;
  44.  
  45. /* Free allocated buffer */
  46.   if (TESTFLAG(fp, _IOMYBUF))
  47.     free((void *) fp->_base);
  48.   if (*sp == NULL)
  49.     free((void *) fp);
  50.  
  51.   return 0;
  52. }
  53.